package Entity;

import java.sql.ResultSet;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

import javax.swing.ComboBoxModel;

import Control.CommonControls;
import Control.MySQLController;

public class Job_Registration extends User{ 
	
	private String jobID; 
	private String jobName; 
	private String jobDesc; 
	private String educationCode; 
	private double salary; 
	private int noSlots; 
	private Date dateCreated; 
	private Date dateUpdated; 
	private String categoryCode; 
	private String employerID; 
	private String jobStatusCode;
	
	public Job_Registration(){}
	//Getters and Setters
	public String getJobID() {
		return jobID;
	}
	public void setJobID(String jobID) {
		this.jobID = jobID;
	}
	public String getJobName() {
		return jobName;
	}
	public void setJobName(String jobName) {
		this.jobName = jobName;
	}
	public String getJobDesc() {
		return jobDesc;
	}
	public void setJobDesc(String jobDesc) {
		this.jobDesc = jobDesc;
	}
	public String getEducationCode() {
		return educationCode;
	}
	public void setEducationCode(String educationCode) {
		this.educationCode = educationCode;
	}
	public double getSalary() {
		return salary;
	}
	public void setSalary(double salary) {
		this.salary = salary;
	}
	public int getNoSlots() {
		return noSlots;
	}
	public void setNoSlots(int noSlots) {
		this.noSlots = noSlots;
	}
	public Date getDateCreated() {
		return dateCreated;
	}
	public void setDateCreated(Date date) {
		this.dateCreated = date;
	}
	public Date getDateUpdated() {
		return dateUpdated;
	}
	public void setDateUpdated(Date date) {
		this.dateUpdated = date;
	}
	public String getCategoryCode() {
		return categoryCode;
	}
	public void setCategoryCode(String categoryCode) {
		this.categoryCode = categoryCode;
	}
	public String getEmployerID() {
		return employerID;
	}
	public void setEmployerID(String employerID) {
		this.employerID = employerID;
	}
	public String getJobStatusCode() {
		return jobStatusCode;
	}
	
	public void setJobStatusCode(String jobStatusCode) {
		this.jobStatusCode = jobStatusCode; 
		
	}
	
	
	public Job_Registration(String jobID, String jobName, String jobDesc,
			String educationCode, double salary, int noSlots,
			Date dateCreated, Date dateUpdated, String categoryCode,
			String employerID, String jobStatusCode) {
		super();
		this.jobID = jobID;
		this.jobName = jobName;
		this.jobDesc = jobDesc;
		this.educationCode = educationCode;
		this.salary = salary;
		this.noSlots = noSlots;
		this.dateCreated = dateCreated;
		this.dateUpdated = dateUpdated;
		this.categoryCode = categoryCode;
		this.employerID = employerID;
		this.jobStatusCode = jobStatusCode;
	}  
	
	public boolean registerJob(){
		boolean success = false;
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
		MySQLController db = new MySQLController();
		db.getConnection();
		String sql = "INSERT INTO ooadpj.job(job_id, name, description, edu_lvl_code, salary, no_slots, date_created, date_updated, category_code, employer_id, job_status_code)";
		sql += "VALUES ('" + jobID + "','" + jobName + "','" + jobDesc + "','" + educationCode + "','" + salary + "','" + noSlots + "','" + sdf.format(dateCreated) + "','" + sdf.format(dateUpdated) + "','" + categoryCode + "','" + employerID + "','" + jobStatusCode + "')";
		if(db.updateRequest(sql)==1)
			success = true;
		db.terminate();
		return success;
	} 
	
	public boolean registerForJob(){
		boolean success = false;
		MySQLController db = new MySQLController();
		db.getConnection();
		String sql = "INSERT INTO ooadpj.job_Registered(job_id, nric)";
		sql += "VALUES ('" + jobID + "','" + CommonControls.getUser().getNric() + "')";
		if(db.updateRequest(sql)==1)
			success = true;
		db.terminate();
		return success;
	} 
	
	public boolean updateJob(){
		boolean success = false;
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd hh:mm:ss");
		MySQLController db = new MySQLController();
		db.getConnection();
		String sql = "UPDATE ooadpj.job";
		sql += " Set name ='" + getJobName() + "', description ='" + getJobDesc() + "', salary = "+ getSalary() + " , no_slots = '" + getNoSlots() + "' , date_updated = '" + sdf.format(dateUpdated)  + "'";
		sql += " WHERE job_id = '" + getJobID() +"'"; 
		if(db.updateRequest(sql)==1)
		success = true;
		db.terminate();
		return success;
	}
	
	public boolean deleteJob(){ 		
		boolean success = false;
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
		MySQLController db = new MySQLController();
		db.getConnection();
		String sql = "UPDATE ooadpj.job";
		sql += " Set job_status_code = 'Unavilable'"; //, date_updated = '" +sdf.format(dateUpdated) + "'" ;
		sql += " WHERE job_id = '" + getJobID() +"'"; 
		if(db.updateRequest(sql)==1)
		success = true;
		db.terminate();
		return success;
	}
	
	
	public static ArrayList<Job_Registration> retrieveJob(){ 
		ArrayList<Job_Registration> jobList = new ArrayList<Job_Registration>();
		
		ResultSet rs = null;
		MySQLController db = new MySQLController();
		db.getConnection(); 
		
		String sql = "SELECT * FROM ooadpj.job "; 
		
		rs = db.readRequest(sql);
		try{
			while(rs.next()){
				Job_Registration job = new Job_Registration(); 
				job.setJobID(rs.getString("job_id"));
				job.setJobName(rs.getString("name"));
				job.setJobDesc(rs.getString("description")); 
				job.setEducationCode(rs.getString("edu_lvl_code")); 
				job.setSalary(rs.getDouble("salary")); 
				job.setNoSlots(rs.getInt("no_slots"));
				job.setCategoryCode(rs.getString("category_code")); 
				job.setJobStatusCode(rs.getString("job_status_code"));
				jobList.add(job);
			}
		}
		catch(Exception e){
			e.printStackTrace();
		}
		finally{
			db.terminate();
		}
		return jobList;
	}
	
	public static ArrayList<Job_Registration> searchJobName(String jobName){ 
		ArrayList<Job_Registration> jobList = new ArrayList<Job_Registration>();
		
		ResultSet rs = null;
		MySQLController db = new MySQLController();
		db.getConnection(); 
		
		String sql = "SELECT * FROM ooadpj.job WHERE name LIKE " + "'%" + jobName + "%'" ; 
		
		rs = db.readRequest(sql);
		try{
			while(rs.next()){
				Job_Registration job = new Job_Registration();
				job.setJobID(rs.getString("job_id"));
				job.setJobName(rs.getString("name"));
				job.setJobDesc(rs.getString("description")); 
				job.setEducationCode(rs.getString("edu_lvl_code")); 
				job.setSalary(rs.getDouble("salary")); 
				job.setNoSlots(rs.getInt("no_slots"));
				job.setCategoryCode(rs.getString("category_code")); 
				job.setJobStatusCode(rs.getString("job_status_code"));
				jobList.add(job);
			}
		}
		catch(Exception e){
			e.printStackTrace();
		}
		finally{
			db.terminate();
		}
		return jobList;
	}
	
	public static ArrayList<Job_Registration> searchEduLvl(String eduCode){ 
		ArrayList<Job_Registration> jobList = new ArrayList<Job_Registration>();
		
		ResultSet rs = null;
		MySQLController db = new MySQLController();
		db.getConnection(); 
		
		String sql = "SELECT * FROM ooadpj.job WHERE edu_lvl_code LIKE " + "'%" + eduCode + "%'" ; 
		
		rs = db.readRequest(sql);
		try{
			while(rs.next()){
				Job_Registration job = new Job_Registration(); 
				job.setJobID(rs.getString("job_id"));
				job.setJobName(rs.getString("name"));
				job.setJobDesc(rs.getString("description")); 
				job.setEducationCode(rs.getString("edu_lvl_code")); 
				job.setSalary(rs.getDouble("salary")); 
				job.setNoSlots(rs.getInt("no_slots"));
				job.setCategoryCode(rs.getString("category_code")); 
				job.setJobStatusCode(rs.getString("job_status_code"));
				jobList.add(job);
			}
		}
		catch(Exception e){
			e.printStackTrace();
		}
		finally{
			db.terminate();
		}
		return jobList;
	}
	
	public static ArrayList<Job_Registration> searchJobCat(String string){ 
		ArrayList<Job_Registration> jobList = new ArrayList<Job_Registration>();
		
		ResultSet rs = null;
		MySQLController db = new MySQLController();
		db.getConnection(); 
		
		String sql = "SELECT * FROM ooadpj.job WHERE category_code LIKE " + "'%" + string + "%'" ; 
		
		rs = db.readRequest(sql);
		try{
			while(rs.next()){
				Job_Registration job = new Job_Registration(); 
				job.setJobID(rs.getString("job_id"));
				job.setJobName(rs.getString("name"));
				job.setJobDesc(rs.getString("description")); 
				job.setEducationCode(rs.getString("edu_lvl_code")); 
				job.setSalary(rs.getDouble("salary")); 
				job.setNoSlots(rs.getInt("no_slots"));
				job.setCategoryCode(rs.getString("category_code")); 
				job.setJobStatusCode(rs.getString("job_status_code"));
				jobList.add(job);
			}
		}
		catch(Exception e){
			e.printStackTrace();
		}
		finally{
			db.terminate();
		}
		return jobList;
	}
		
		}
	
 
		

